package com.ruoyi.customerSample.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.customerSample.domain.InspectedUnitParams;
import com.ruoyi.customerSample.service.IInspectedUnitParamsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 受检设备铭牌参数Controller
 * 
 * @author ruoyi
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/custSam/unitParams")
@Api(value="受检设备铭牌",tags = "受检设备铭牌接口")
public class InspectedUnitParamsController extends BaseController
{
    @Autowired
    private IInspectedUnitParamsService inspectedUnitParamsService;

    /**
     * 查询受检设备铭牌参数列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询受检设备铭牌参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(InspectedUnitParams inspectedUnitParams)
    {
        startPage();
        List<InspectedUnitParams> list = inspectedUnitParamsService.selectInspectedUnitParamsList(inspectedUnitParams);
        return getDataTable(list);
    }

    /**
     * 导出受检设备铭牌参数列表
     */
    @Log(title = "受检设备铭牌参数", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InspectedUnitParams inspectedUnitParams)
    {
        List<InspectedUnitParams> list = inspectedUnitParamsService.selectInspectedUnitParamsList(inspectedUnitParams);
        ExcelUtil<InspectedUnitParams> util = new ExcelUtil<InspectedUnitParams>(InspectedUnitParams.class);
        return util.exportExcel(list, "params");
    }

    /**
     * 获取受检设备铭牌参数详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取受检设备铭牌参数详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(inspectedUnitParamsService.selectInspectedUnitParamsById(id));
    }

    /**
     * 根据受检设备Id获取受检设备铭牌参数详细信息
     */
    @GetMapping(value = "/getInfoByEquId/{equId}")
    @ApiOperation(value="获取受检设备铭牌参数详细信息")
    @ApiImplicitParam(paramType="query", name = "equId", value = "设备Id", required = true, dataType = "String")
    public AjaxResult getInfoByEquId(@PathVariable("equId") String equId)
    {
        List<InspectedUnitParams> params = inspectedUnitParamsService.selectInspectedUnitParamsByEquId(equId);
        if(params.size() > 0){
            //数据拼接
            List<Map<String, Object>> paramList = new ArrayList<Map<String, Object>>();
            List<String> nameplates = new ArrayList<String>();
            //获取大类
            for(InspectedUnitParams inspectedUnitParams : params){
                if(!nameplates.contains(inspectedUnitParams.getNameplateB())){
                    nameplates.add(inspectedUnitParams.getNameplateB());
                }
            }
            for(String nameplateB : nameplates){
                Map<String, Object> pMap = new HashMap<String, Object>();
                pMap.put("nameplateB", nameplateB);
                InspectedUnitParams ins = new InspectedUnitParams();
                ins.setEquId(equId);
                ins.setNameplateB(nameplateB);
                List<InspectedUnitParams> values = inspectedUnitParamsService.selectInspectedUnitParamsList(ins);
                List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                for(InspectedUnitParams pValue : values){
                    Map<String, String> maps = new HashMap<String, String>();
                    maps.put("nameplateS", pValue.getNameplateS());
                    maps.put("parameters", pValue.getParameters());
                    list.add(maps);
                }
                pMap.put("data", list);
                paramList.add(pMap);
            }
            return AjaxResult.success(paramList);
        }
            return AjaxResult.success();
    }

    /**
     * 新增受检设备铭牌参数
     */
    @Log(title = "受检设备铭牌参数", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增受检设备铭牌参数")
    public AjaxResult add(@ApiParam(name="受检设备铭牌参数",value="传入json格式",required=true)@RequestBody InspectedUnitParams inspectedUnitParams)
    {
        return toAjax(inspectedUnitParamsService.insertInspectedUnitParams(inspectedUnitParams));
    }

    /**
     * 批量新增受检设备铭牌参数
     */
    @PostMapping("/addBatch")
    @ApiOperation(value="批量新增受检设备铭牌参数")
    public AjaxResult addBatch(@ApiParam(name="受检设备铭牌集合",value="传入json格式",required=true)@RequestBody List<InspectedUnitParams> inspectedUnitParams)
    {
        for(InspectedUnitParams ins : inspectedUnitParams){
            ins.setId(IdUtils.fastSimpleUUID());
        }
        return toAjax(inspectedUnitParamsService.insertInspectedUnits(inspectedUnitParams));
    }

    /**
     * 修改受检设备铭牌参数
     */
    @Log(title = "受检设备铭牌参数", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBatch")
    @ApiOperation(value="修改受检设备铭牌参数")
    public AjaxResult edit(@ApiParam(name="受检设备铭牌参数",value="传入json格式",required=true)@RequestBody List<InspectedUnitParams> inspectedUnitParams)
    {
        //获取设备Id和单位Id
        String equId = inspectedUnitParams.get(0).getEquId();
        String ownershipid = inspectedUnitParams.get(0).getOwnershipid();
        //删除铭牌参数
        int ret = inspectedUnitParamsService.deleteInspectedUnitParamsByEquId(equId, ownershipid);
        if(ret > 0){
            //批量插入数据
            for(InspectedUnitParams ins : inspectedUnitParams){
                ins.setId(IdUtils.fastSimpleUUID());
            }
            return toAjax(inspectedUnitParamsService.insertInspectedUnits(inspectedUnitParams));
        }
        return AjaxResult.error("修改失败！");
    }

    /**
     * 删除受检设备铭牌参数
     */
    @Log(title = "受检设备铭牌参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除受检设备铭牌参数")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(inspectedUnitParamsService.deleteInspectedUnitParamsByIds(ids));
    }
}
